package zmee.pur.tnd.service;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.scm.pds.common.change.HandleEvent;
import kd.scm.pds.common.change.HandleResult;
import kd.scm.pds.common.change.IDataHandleService;
import kd.scm.pds.common.util.TemplateUtil;
import kd.scm.src.common.util.SrcBidChangeUtil;

import java.util.Date;

/**
 * @Author: xw
 * @Date: 2022/11/14/9:41
 * @Description: 答疑截止时间 变更处理
 */
public class AnswerDateHandleServiceImpl implements IDataHandleService {

    public AnswerDateHandleServiceImpl() {
    }

    @Override
    public HandleResult handle(HandleEvent handleEvent) {
        String message = ResManager.loadKDString("寻源项目时间变更失败", "SrcBidDateHandleService_3", "scm-src-common", new Object[0]);
        HandleResult result = new HandleResult();
        DynamicObject bidChangeObj = handleEvent.getObj();
        DynamicObject project = bidChangeObj.getDynamicObject("project");
        DynamicObject projectObj = BusinessDataServiceHelper.loadSingle("src_projectf7", "id,answerdate", new QFilter[]{new QFilter("id", "=", project.getPkValue())});
        DynamicObject compDataObj = TemplateUtil.getCompData(bidChangeObj, "src_timechg");
        Date newreplydate = compDataObj.getDate("newreplydate");
        if (null != newreplydate) {
            projectObj.set("answerdate", newreplydate);
        }
        SaveServiceHelper.save(new DynamicObject[]{projectObj});

        result.setSuccess(true);
        message = ResManager.loadKDString("寻源项目时间变更成功", "SrcBidDateHandleService_2", "scm-src-common", new Object[0]);
        result.setMessage(message);
        this.handleLog(handleEvent, message);
        return result;
    }

    public void handleLog(HandleEvent handleEvent, String message) {
        DynamicObject bidChange = handleEvent.getObj();
        SrcBidChangeUtil.log(bidChange, message);
    }
}
